<?php

	/*
	 * All rights Reserved, Designed By www.youyacao.com <br>
	 * @Description:视频 <br>
	 * @version 蜻蜓C影视追剧系统  <br>
	 * @author:成都市一颗优雅草科技有限公司  <br>
	 * 注意：使用我司开源源代码请遵循license文件的协议仅供个人非盈利使用，禁止用于其他的商业用途
	 * 需要商业用途或者定制开发等可访问songshu.youyacao.com  联系QQ:2853810243 422108995 23625059584
	 * 正版系统查询系统 zhengbancha.youyacao.com
	 */
namespace App\Admin\Controllers;

use App\Admin\Repositories\Video;
use App\Events\sendSubjectEcent;
use App\Models\Episode;
use App\Models\Type;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Storage;

class VideoController extends AdminController
{
    private $tui;
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Video(['juji']), function (Grid $grid) {
            $grid->model()->orderByDesc('created_at');
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('type_id')->display(function ($type_id){
                $type=json_decode($type_id,true);
                $typemodel=Type::whereIn('id',$type)->pluck('title')->toArray();
                if(count($typemodel)==1){
                    return $typemodel[0];
                }else{
                    return implode("/",$typemodel);
                }
            });
            $grid->column('juji.name','剧集');
            $grid->column('cover')->image('',100,100);
            $grid->column('cover_crosswise')->image('',100,100);
            $grid->column('url')->video(function (\Abovesky\DcatAdmin\MediaPlayer\Grid\VideoDisplayer $video){
                // 自定义弹窗标题
                $video->title('预览视频');
                // 自定义按钮文字
                $video->button('点击预览');
                // 自定义按钮图标
                $video->icon('fa fa-play');
                // 自定义服务器地址
                $video->server(env('APP_URL').'/uploads');
            });
            $grid->async();
            $grid->column('sort')->editable();
            $grid->column('desc')->substr(0, 20);
            $grid->column('status')->switch();
            $grid->column('is_fee')->switch();
            $grid->column('price')->editable();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->quickSearch(['name','juji.name'])->placeholder('可搜索剧集名称或者视频名称');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Video(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('type_id');
            $show->field('episodes_id');
            $show->field('cover');
            $show->field('cover_crosswise');
            $show->field('url');
            $show->field('sort');
            $show->field('desc');
            $show->field('status');
            $show->field('is_fee');
            $show->field('price');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Video(), function (Form $form) {
            $form->display('id');
            $form->select('episodes_id')->options(Episode::pluck('name','id'))->required();
            $form->text('name')->required();
//            $form->text('type_id');
            $form->tree('type_id', '分类')
                ->setTitleColumn('title')
                ->treeState(false)
                ->nodes(\App\Models\Type::where('identify','video')->get()->toArray())
                ->saving(function ($v) {
                    // 转为 , 隔开的字符串
                    if(!empty($v)){
                        return json_encode($v);
                    }else{
                        return 0;
                    }
                })
                ->required();
            $form->image('cover')->accept('jpg,png,gif,jpeg')->autoUpload()->saveFullUrl()->required();
            $form->image('cover_crosswise')->accept('jpg,png,gif,jpeg')->autoUpload()->saveFullUrl()->required();
            if ($form->isCreating()) {
                $form->radio('test', '请选择上传类型')
                    ->when(1, function (Form $form) {
                        $form->file('url1', '视频源')
//                            ->chunkSize(1024)
                            ->maxSize(config("base.videoMaxSize") * 1024)
                            ->accept('mp4,mov')
                            ->autoUpload()
                            ->saveFullUrl();
                    })->when(2, function (Form $form) {
                        $form->url('url2', '视频源');
                    })->options([1 => '本地上传', 2 => '外部地址'])->default(1)->required();
            } elseif ($form->isEditing()) {
                $form->file('url');
            }
            $form->hidden('url');
            $form->number('sort','当前第几集')->required();
            $form->textarea('desc')->required();
            $form->radio('status')->options([0=>'待上架',1=>'上架'])->default(1)->required();
            $form->radio('is_fee')->options([0=>'不收费',1=>'收费'])->default(0)->required();
            $form->number('price')->help('(元)')->required();
            $form->radio('sendsub')->options([0=>'不推送',1=>'推送'])->default(1)->required();

            $form->display('created_at');
            $form->display('updated_at');
        })->saving(function (Form $form){
            if($form->url1!=null && $form->url2!=null){
                return $form->response()->error('视频地址错误，请刷新当前页面！');
            }
            if($form->url1!=null){
                $form->url=$form->url1;
            }elseif ($form->url2!=null){
                $form->url=$form->url2;
            }
            if ($form->sendsub){
                $this->tui=$form->sendsub;
            }
            $form->deleteInput('url1');
            $form->deleteInput('sendsub');
            $form->deleteInput('url2');
            $form->deleteInput('test');
        })->saved(function (Form $form,$id){
            if ($form->isCreating()) {
                Episode::where('id', $form->episodes_id)->update(['blues' => \App\Models\Video::where('episodes_id', $form->episodes_id)->count()]);
                if($this->tui){
                    event(new sendSubjectEcent($form->episodes_id,\App\Models\Video::TEMPLATE_ID['sendsub'],$id));
                }
                if (!$id) {
                    return $form->error('数据保存失败');
                }
            }
        });
    }
}
